/**
 * 格式化 2位数字
 */
export const formatNum = (n: string | number) => `${n}`.padStart(2, "0");

/**
 * 格式化 2位小数
 */
export const formatDec = (n: string | number) => Number(n).toFixed(2);

/**
 * 格式化 时分秒
 */
export const formatHHMMSS = (n: number) => {
  const date = formatDHMS(n);
  const hour = formatNum(date.hour);
  const minute = formatNum(date.minute);
  const second = formatNum(date.second);
  return `${hour}:${minute}:${second}`;
};

/**
 * 格式化 分秒
 */
export const formatMMSS = (n: number) => {
  const date = formatDHMS(n);
  const minute = formatNum(date.minute);
  const second = formatNum(date.second);
  return `${minute}:${second}`;
};

/**
 * 格式化 日期
 */
export const formatDHMS = (n: number) => {
  const day = Math.floor(n / (24 * 60 * 60));
  n = n % (24 * 60 * 60);
  const hour = Math.floor(n / (60 * 60));
  n = n % (60 * 60);
  const minute = Math.floor(n / 60);
  n = n % 60;
  return { day, hour, minute, second: n };
};

/**
 * 格式化字 符串指定位置替换成*
 */
export const formatStr = (n: string, preNum = 6, sufNu = 4) => {
  const pre = n.slice(0, preNum);
  const len = n.length;
  const endStart = n.length - sufNu;
  return `${pre.padEnd(endStart, "*")}${n.slice(endStart, len)}`;
};
